package com.csdj.essay.controller;


import com.csdj.essay.common.WebResult;
import com.csdj.essay.common.WebResultMsgCode;
import com.csdj.essay.entity.Article;
import com.csdj.essay.entity.TQuestion;
import com.csdj.essay.service.TQuestionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 问题表 前端控制器
 * </p>
 *
 * @author 刘宇
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/t-question")
public class TQuestionController extends BaseController{

    @Resource
    private TQuestionService tQuestionService;

    /**
     * 查询全部，推荐模块
     * @return
     */
    @GetMapping("/getInfo")
    public WebResult getInfo(){
        List<TQuestion> list = tQuestionService.getInfo();
        if(list != null){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("tQuestion",list);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }

    /**
     * 查询关注的人发布的问题
     * @return
     */
    @GetMapping("/getInfoFollow")
    public WebResult getInfoFollow(HttpServletRequest request){
        int uid = getUid(request);
        List<Article> list = tQuestionService.getInfoFollow(uid);
        if(list != null) {
            return new WebResult(WebResultMsgCode.SUCCESS).putData("list", list);
        }
        //返回null 前端做判断，为null需要显示（您还没有关注的人）
        return null;
    }

    /**
     * 连表查询，主页热榜，根据访问人数进行倒序
     * @return
     */
    @GetMapping("/getInfoHotList")
    public WebResult getInfoHotList(){
        List<Map<String,Object>> list = tQuestionService.getInfoHotList();
        if(list != null){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("list", list);
        }
        return null;
    }
}
